package com.huo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.container.page.PageHandler;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huo.dao.CheckGroupDao;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.pojo.CheckGroup;
import com.huo.service.CheckGroupService;
import com.huo.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * @Description: 新增
     * @Author: 汪青松
     * @Param: [checkGroup, checkitemIds]
     * @Return: void
     * @CreateDate: 2021/1/18 14:43
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        setCheckGroupAndCheckitemIds(checkGroup.getId(), checkitemIds);
    }


    /**
     * @Description: 分页查询
     * @Author: 汪青松
     * @Param: [queryPageBean]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/18 17:41
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * @Description: 根据id获取检查组信息
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 14:55
     */
    @Override
    public CheckGroup findById(Integer checkgroupId) {
        return checkGroupDao.findById(checkgroupId);
    }

    /**
     * @Description: 根据检查组id查询检查项ids
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 15:01
     */
    @Override
    public Integer[] findCheckitemIdsById(Integer checkgroupId) {
        return checkGroupDao.findCheckitemIdsById(checkgroupId);
    }

    /**
     * @Description: 编辑
     * @Author: 汪青松
     * @Param: [checkitemIds, checkGroup]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 16:05
     */
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.deleteRelation(checkGroup.getId());
        setCheckGroupAndCheckitemIds(checkGroup.getId(), checkitemIds);
        checkGroupDao.edit(checkGroup);
    }

    /**
     * @Description: 删除
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 17:12
     */
    @Override
    public void delete(Integer checkgroupId) {
        long count = checkGroupDao.countByCheckgroupId(checkgroupId);
        if (count > 0) {
            throw new RuntimeException("此检查组有关联的检查项，不可删除");
        }
        checkGroupDao.delete(checkgroupId);
    }

    /**
     * @Description: 查询所有检查组
     * @Author: 汪青松
     * @Param: []
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/21 16:25
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * @Description: 设置检查组和检查项之间的关系
     * @Author: 汪青松
     * @Param: [checkgroupId, checkitemIds]
     * @Return: void
     * @CreateDate: 2021/1/18 14:47
     */
    private void setCheckGroupAndCheckitemIds(Integer checkgroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.setCheckGroupAndCheckitemIds(checkgroupId, checkitemId);
            }
        }
    }
}
